package com.personal.book1;

import com.personal.book1.component.ComponentTest;
import com.personal.book1.config.ConfigurationTest;
import com.personal.book1.controller.BookControllerTest;
import com.personal.book1.controller.ControllerTest;
import com.personal.book1.model.BookInfo;
import com.personal.book1.repository.RepositoryTest;
import com.personal.book1.service.ServiceTest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.personal")
@SpringBootApplication
public class Book1Application {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(Book1Application.class, args);

		//BookControllerTest bookControllerTest = context.getBean(BookControllerTest.class);
		//bookControllerTest.hello();
	}

	public static void main2(String[] args) {
		ApplicationContext context = SpringApplication.run(Book1Application.class, args);

		// 从上下文中获取 ControllerTest 的Bean
		ControllerTest controllerTest = context.getBean("controllerTest", ControllerTest.class);
		controllerTest.hello();

	}

	public static void main1(String[] args) {
		// 通过注解 Controller 和 类 获取 Bean
		ApplicationContext context = SpringApplication.run(Book1Application.class, args);
		ControllerTest bean = context.getBean(ControllerTest.class);
		bean.hello();

		// 通过注解 Service 和 bean名称 获取 Bean
		ServiceTest serviceTest = (ServiceTest)context.getBean("serviceTest");
		serviceTest.hello();

		// 通过注解 Component 和 bean名称 获取 Bean
		ComponentTest componentTest = context.getBean("component", ComponentTest.class);
		componentTest.hello();

		// 通过注解 Repository 获取 Bean
		RepositoryTest repositoryTest = context.getBean("repositoryTest", RepositoryTest.class);
		repositoryTest.hello();

		// 通过注解 Configuration 获取 Bean
		ConfigurationTest configurationTest = context.getBean("configurationTest", ConfigurationTest.class);
		configurationTest.hello();

		// 通过方法注解 Bean 获取 我们手动创建的，但是由Spring来管理的对象
		BookInfo bookInfo1 = context.getBean("b1", BookInfo.class);
		System.out.println(bookInfo1);

		BookInfo bookInfo2 = context.getBean("b2", BookInfo.class);
		System.out.println(bookInfo2);

		BookInfo bookInfo3 = context.getBean("b3", BookInfo.class);
		System.out.println(bookInfo3);
	}

}
